﻿using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using Newtonsoft.Json;
using RestSemitaService.Models;

namespace RestSemitaService.Controllers
{
	public class CustomerRatingController : ApiController
    {
        // GET api/feedback
        public IEnumerable<string> Get()
        {
            return new string[] { "value1", "value2" };
        }

       
		/// <summary>
		/// Update invoice_head set Feedback where invoice status
		/// </summary>
		/// <param name="invoice_no"></param>
		/// <param name="feedback_score"></param>
		/// <returns></returns>
		public string Get(string invoice_no, string feedback_score)
		{
			try
			{
				SqlConnection thisConnection = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["connectionString"].ConnectionString);
				thisConnection.Open();
				SqlCommand thisCommand = thisConnection.CreateCommand();
				thisCommand.CommandText = @"update	trans1 set feedback_score = @feedback_score
											From	delivery_tran trans1
											Where	id = (	select	max(id) as max_invoice_id 
															from	delivery_tran trans2 
															Where	invoice_no = @invoice_no)
													and isnull(feedback_score,0) = 0";

				thisCommand.Parameters.AddWithValue("@invoice_no", invoice_no);
				thisCommand.Parameters.AddWithValue("@feedback_score", feedback_score);

				if (thisCommand.ExecuteNonQuery() > 0)
				{
					thisConnection.Close();
					return "true";
				}
				else
				{
					thisConnection.Close();
					return "false";
				}
			}
			catch (SqlException e)
			{
				Console.WriteLine(e.Message);
				return e.StackTrace + " \n >>>>>>>>>>>>>>>>> \n" + e.Message;
			}
		}

        // POST api/feedback
        // update status and feed back
		public void Post([FromBody]string value)
        {
            
        }

        // PUT api/feedback/5
        public void Put(int id, [FromBody]string value)
        {
        }

        // DELETE api/feedback/5
        public void Delete(int id)
        {
        }
    }
}
